package com.sun.showcase.biz.testcase.service.impl;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.sun.showcase.biz.testcase.dao.TaskGroupDao;
import com.sun.showcase.biz.testcase.service.TaskGroupService;
import com.sun.showcase.client.testcase.domain.TaskGroup;
import com.sun.showcase.client.testcase.query.TaskGroupQuery;
import com.sun.showcase.pojo.DataGrid;
import com.sun.showcase.pojo.Pager;
import com.sun.showcase.utils.LoginContextHolder;
import com.sun.showcase.utils.StringConstant;
@Service("taskGroupService")
@Transactional
public class TaskGroupServiceImpl implements TaskGroupService{
	@Autowired
	private TaskGroupDao taskGroupDao;
	
	public void setTaskGroupDao(TaskGroupDao dao) {
		this.taskGroupDao = dao;
	}

	public DataGrid datagrid(TaskGroupQuery taskGroupQuery) {
		DataGrid j = new DataGrid();
		Pager<TaskGroup> pager  = find(taskGroupQuery);
		j.setRows(getQuerysFromEntitys(pager.getRecords()));
		j.setTotal(pager.getTotalRecords());
		return j;
	}

	private List<TaskGroupQuery> getQuerysFromEntitys(List<TaskGroup> taskGroups) {
		List<TaskGroupQuery> taskGroupQuerys = new ArrayList<TaskGroupQuery>();
		if (taskGroups != null && taskGroups.size() > 0) {
			for (TaskGroup tb : taskGroups) {
				TaskGroupQuery b = new TaskGroupQuery();
				BeanUtils.copyProperties(tb, b);
				taskGroupQuerys.add(b);
			}
		}
		return taskGroupQuerys;
	}

	private Pager<TaskGroup> find(TaskGroupQuery taskGroupQuery) {
		Page<TaskGroup> page = PageHelper.startPage(taskGroupQuery.getPage().intValue(), taskGroupQuery.getRows().intValue());
		List<TaskGroup> list = taskGroupDao.findList(taskGroupQuery);
		Pager<TaskGroup> pager = new Pager<TaskGroup>();
		pager.setRecords(list);
		pager.setTotalRecords(page.getTotal());
		return  pager;
		
	}
	


	public TaskGroup add(TaskGroupQuery taskGroupQuery) {
		TaskGroup t = new TaskGroup();
		BeanUtils.copyProperties(taskGroupQuery, t);
		taskGroupDao.save(t);
		taskGroupQuery.setId(t.getId());
		return t;
		
	}

	public void update(TaskGroupQuery taskGroupQuery) {
		TaskGroup t = taskGroupDao.getById(taskGroupQuery.getId());
	    if(t != null) {
	    	BeanUtils.copyProperties(taskGroupQuery, t);
	    	taskGroupDao.update(t);
		}
	}
	
	/**
	 * 根据条件更新部分字段   新建一个query封装需要更新的字段
	 * searchMap封装更新条件
	 * */
	public void updatePart(TaskGroupQuery taskGroupQuery) {
	    taskGroupDao.updatePart(taskGroupQuery);
	}

	public void delete(java.lang.Long[] ids) {
		taskGroupDao.deleteAc(ids);
	}
	
	//逻辑删除 更新状态位
	public void deletePt(java.lang.Long[] ids) {
		taskGroupDao.deletePt(ids);
	}

	public TaskGroup get(TaskGroupQuery taskGroupQuery) {
		return taskGroupDao.getById(new Long(taskGroupQuery.getId()));
	}

	public TaskGroup get(Long id) {
		return taskGroupDao.getById(id);
	}

	
	public List<TaskGroupQuery> listAll(TaskGroupQuery taskGroupQuery) {
	    List<TaskGroup> list = taskGroupDao.findList(taskGroupQuery);
		List<TaskGroupQuery> listQuery =getQuerysFromEntitys(list) ;
		return listQuery;
	}
	
	
}
